﻿using System;
using System.Windows.Input;
namespace WPFExcelReport
{

    public partial class ViewModelBase 
    {
        public DelegateCommand QueryCommand { get; set; }
        public DelegateCommand CopyCommand { get; set; }
        public DelegateCommand SaveCommand { get; set; }
        public DelegateCommand DeleteCommand { get; set; } 
    }
   
    public class DelegateCommand : ICommand
    {
        public Predicate<object> CanExecuteDelegate { get; set; }
        public Action<object> ExecuteDelegate { get; set; }
        private Action action;
        public bool CanExecute(object parameter)
        {
            if (CanExecuteDelegate != null)
                return CanExecuteDelegate(parameter);
            return true; // if there is no can execute default to true
        } 

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public DelegateCommand(Action action)
        {
            this.action = action;
        }

        public DelegateCommand(Action<Object> action)
        {
            this.ExecuteDelegate = action;
        }  

        public void Execute(object parameter)
        {
            if (action != null)
            {
                action();
            }
            if (ExecuteDelegate != null)
            {
                ExecuteDelegate.Invoke(parameter);
            }
        }
    }
}
